import $ from 'jquery';
class Mutil{
	request(param) {
		return new Promise((resolve, reject) => {
			$.ajax({
				type     : param.type    || 'get',
				url      : param.url     || '',
				dataType : param.dataType || 'json',
				data     : param.data    || null,
				success  : res => {
					if( res.status === 0 ){
						typeof resolve === 'function' && resolve(res.data, res.msg);
					}
					//没有登录，强制登录
					else if(res.status === 10){
						this.doLogin();
					}else{
						typeof reject === 'function' && reject(res.msg || res.data);
					}
				},
				error    : err => {
					typeof reject === 'function' && reject(err.statusText);
				}
			})
		})
		
	}
	doLogin() {
		window.location.href="/login?redirect=" + encodeURIComponent(window.location.pathname);
	}
	//获取url参数
	getUrlParam(name){
		//xxx.com?param=123&param1=234
		let queryString = window.location.search.split('?')[1] || '',
			reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
			result = queryString.match(reg);
			return result ?  encodeURIComponent(result[2]) : null;
	}
	//成功提示
	successTips(successMag){
		alert(successMag || '操作成功')
	}
	//错误提示
	errorTips(errMag){
		alert(errMag || '好像哪里不对了~~~')
	}//存储
	setStorage(name, data){
		let dataType = typeof data;
		//json对象
		if( dataType === 'object'){
			window.localStorage.setItem(name, JSON.stringify(data));
		}
		//基础类型
		else if(['number', 'string', 'boolean'].indexOf(dataType) >= 0){
			window.localStorage.setItem(name, data);
		}
		else{
			alert('该类型不能用于本地存储');
		}
	}//取出存储内容
	getStorage(name){
		let data = window.localStorage.getItem(name);
		if(data){
			return JSON.parse(data);
		}
		else{
			return ''
		}
	}
	//删除本地存储
	removeStorage(name){
		window.localStorage.removeItem(name);
	}
}


export default Mutil;